package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.common.RedisConst;
import com.itheima.health.dao.*;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.pojo.SetmealCheckgroup;
import com.itheima.health.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

@DubboService
@Slf4j
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealCheckgroupMapper setmealCheckgroupMapper;
    @Autowired
    private CheckGroupMapper checkGroupMapper;
    @Autowired
    private CheckGroupItemMapper checkGroupItemMapper;
    @Autowired
    private CheckItemMapper checkItemMapper;

    /**
     * @Author: 位帅帅
     * @Date: 19:31 2021/1/6
     * @Params: [checkgroupIds, setmeal]
     * @RerurnType: void
     * @Description: 添加套餐
    */
    @Override
    @Transactional
    public void add(Integer[] checkgroupIds, Setmeal setmeal) {
        log.info("[套餐-添加]checkgroupIda: {}, setmeal: {}",checkgroupIds,setmeal);
        setmealMapper.insertSelective(setmeal);
        for (Integer checkgroupId : checkgroupIds) {
            SetmealCheckgroup setmealCheckgroup = new SetmealCheckgroup();
            setmealCheckgroup.setSetmealId(setmeal.getId());
            setmealCheckgroup.setCheckgroupId(checkgroupId);
            setmealCheckgroupMapper.insertSelective(setmealCheckgroup);
        }
    }

    /**
     * @Author: 位帅帅
     * @Date: 19:31 2021/1/6
     * @Params: [queryPageBean]
     * @RerurnType: com.itheima.health.entity.PageResult
     * @Description: 分页查询套餐
    */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        log.info("[套餐-分页查询]queryPageBean: {}",queryPageBean);
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<Setmeal> page = setmealMapper.findPage(queryPageBean.getQueryString());
        PageResult pageResult = new PageResult(page.getTotal(), page.getResult());
        log.info("[套餐-分页查询]pageResult: {}",pageResult);
        return pageResult;
    }

    /**
     * @Author: 位帅帅
     * @Date: 20:28 2021/1/6
     * @Params: [id]
     * @RerurnType: void
     * @Description: 删除套餐
    */
    @Transactional
    @Override
    public void delete(Integer id) {
        setmealCheckgroupMapper.deleteBySetmealId(id);
        setmealMapper.deleteByPrimaryKey(id);
    }

    /**
     * @Author: 位帅帅
     * @Date: 20:54 2021/1/6
     * @Params: [id]
     * @RerurnType: com.itheima.health.pojo.Setmeal
     * @Description: 根据id查询套餐
    */
    @Override
    public Setmeal findById(Integer id) {
        log.info("[套餐-id查询]id: {}",id);
        Setmeal setmeal = setmealMapper.selectByPrimaryKey(id);
        List<Integer> checkGroupIds = setmealCheckgroupMapper.findCheckGroupIdsBySetmealId(id);
        List<CheckGroup> checkGroups = new ArrayList<>();
        for (Integer checkGroupId : checkGroupIds) {
            CheckGroup checkGroup = checkGroupMapper.selectByPrimaryKey(checkGroupId);
            List<CheckItem> checkItems = new ArrayList<>();
            List<Integer> checkItemIds = checkGroupItemMapper.findCheckItemIdsByCheckGroupId(checkGroupId);
            for (Integer checkItemId : checkItemIds) {
                CheckItem checkItem = checkItemMapper.selectByPrimaryKey(checkItemId);
                checkItems.add(checkItem);
            }
            checkGroup.setCheckItems(checkItems);
            checkGroups.add(checkGroup);
        }
        setmeal.setCheckGroups(checkGroups);
        log.info("[套餐-id查询]setmeal: {}",setmeal);
        return setmeal;
    }

    /**
     * @Author: 位帅帅
     * @Date: 21:05 2021/1/6
     * @Params: [id]
     * @RerurnType: java.util.List<java.lang.Integer>
     * @Description: 根据套餐id查询检查组id
    */
    @Override
    public List<Integer> findCheckGroupIdsBySetmealId(Integer id) {
        log.info("[套餐-根据套餐id查询检查组id]id: {}",id);
        List<Integer> checkGroupIds = setmealCheckgroupMapper.findCheckGroupIdsBySetmealId(id);
        log.info("[套餐-根据套餐id查询检查组id]checkGroupIds: {}" + checkGroupIds);
        return checkGroupIds;
    }

    /**
     * @Author: 位帅帅
     * @Date: 21:20 2021/1/6
     * @Params: [checkgroupIds, setmeal]
     * @RerurnType: void
     * @Description: 编辑套餐
    */
    @Override
    @Transactional
    public void edit(Integer[] checkgroupIds, Setmeal setmeal) {
        log.info("[套餐-编辑]checkgroupIds: {}, setmeal: {}",checkgroupIds,setmeal);
        setmealCheckgroupMapper.deleteBySetmealId(setmeal.getId());
        setmealMapper.updateByPrimaryKeySelective(setmeal);
        for (Integer checkgroupId : checkgroupIds) {
            SetmealCheckgroup setmealCheckgroup = new SetmealCheckgroup();
            setmealCheckgroup.setSetmealId(setmeal.getId());
            setmealCheckgroup.setCheckgroupId(checkgroupId);
            setmealCheckgroupMapper.insertSelective(setmealCheckgroup);
        }
    }

    /**
     * @Author: 位帅帅
     * @Date: 14:17 2021/1/9
     * @Params: [img]
     * @RerurnType: java.lang.Long
     * @Description: 统计图片使用次数
    */
    @Override
    public Integer countByImg(String img) {
        log.info("[套餐-统计图片使用次数]img: {}",img);
        Example example = new Example(Setmeal.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("img",img);
        int count = setmealMapper.selectCountByExample(example);
        log.info("[套餐-统计图片使用次数]count: {}",count);
        return count;
    }

    /**
     * @Author: 位帅帅
     * @Date: 15:55 2021/1/10
     * @Params: []
     * @RerurnType: java.util.List<com.itheima.health.pojo.Setmeal>
     * @Description: 查询所有套餐
    */
    @Override
    public List<Setmeal> findAll() {
        List<Setmeal> setmeals = setmealMapper.selectAll();
        log.info("[套餐-查询所有]setmeals: {}",setmeals);
        return setmeals;
    }
}
